// ve = vue element
let ve_business;
let ve_business_info;
$(function () {
   ve_business = new Vue({
       el: "#business-data-box",
       data: {
           page_index: 1,
           page_num: 1,
           business_providers: [],
           pointed_city: "",
           pointed_business_id: -1,
           businesses: [],
           go: 1
       },
       methods: {
           get_paging_data(page) {
               ve_business.get_pages_amount();
               ve_business.get_businesses_data();

               if (page < 1)
                   page = 1;
               else if (page > ve_business.page_num)
                   page = ve_business.page_num;

               ajax_get("/provider/page/" + (page - 1),
                   {
                       city: ve_business.pointed_city,
                       business_id: ve_business.pointed_business_id
                   },
                   function (response) {
                        ve_business.business_providers = response;
                   }, "Getting paging data of business providers")
           },
           get_pages_amount() {
               ajax_get("/provider/page",
                   {
                       city: ve_business.pointed_city,
                       business_id: ve_business.pointed_business_id
                   },
                   function (response) {
                       ve_business.page_num = response;
                   }, "Getting pages amount of business providers")
           },
           render_modal_box(id) {
               ve_business_info.business_provider = ve_business.business_providers.find(business => business.id === id);

               ve_business_info.reserved = false;
               for (let i = 0; i < ve_user.reservations.length; i++) {
                   if (ve_user.reservations[i].providerId === ve_business_info.business_provider.id) {
                       ve_business_info.reserved = true;
                       ve_business_info.reservation_id = ve_user.reservations[i].id;
                       break;
                   }
               }
           },
           reset_search() {
               ve_business.pointed_city = "";
               ve_business.pointed_business_id = -1;
               ve_business.get_paging_data(0)
           },
           get_businesses_data() {
               ajax_get("/business", {},
                   function (response) {
                       ve_business.businesses = response;
                   }, "Getting all businesses")
           }

       }
   });

   ve_business_info = new Vue({
       el: "#provider-info",
       data: {
           business_provider: {},
           reserved: false,
           reservation_id: -1
       },
       methods: {
           reset_info() {
               ve_business_info.business_provider = {}
           },
           reservation() {
               console.log("VBI " + ve_business_info.reserved);
               if (ve_business_info.reserved) {
                   ajax_delete("/user/reservation/" + ve_business_info.reservation_id, function (response) {
                       if (response) {
                           console.log("VBI A");
                           ve_business_info.reserved = false;
                           ve_user.get_reservation_info();
                       }
                   }, "Deleting reservation")
               } else {
                   ajax_post("/user/reservation", {
                       provider: {
                           id: ve_business_info.business_provider.id
                       },
                       user: {
                           id: ve_nav_bar.validation.id
                       }
                   }, function (response) {
                       console.log("VBI B");
                       if (response) {
                           ve_business_info.reserved = true;
                       }
                       ve_user.get_reservation_info();
                   }, "Adding reservation")
               }
           },
           getReservationId() {
               console.log("VBI getID " +ve_user.reservations.length);
               for (let i = 0; i < ve_user.reservations.length; i++) {
                   if (ve_user.reservations[i].providerId === ve_business_info.business_provider.id) {
                       ve_business_info.reservation_id = ve_user.reservations[i].id;
                       console.log("R ID " + ve_business_info.reservation_id);
                       break;
                   }
               }
           }
       },
       computed: {
           reservation_text() {
               return this.reserved ? "取消预订" : "预订";
           },
           is_logged_in() {
               return ve_nav_bar.validation.id > 0;
           }
       }
   });
});